package com.china08.yxyapi.service.manage.tec.teaching.homework.workstatis1.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.api.manage.tec.teaching.homework.workstatis.WorkFinishRateV1Controller.RespModel;
import com.china08.yxyapi.entity.mg.homework.HomeworkClassForDayReport;
import com.china08.yxyapi.repository.mg.homework.HomeworkClassForDayReportRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.workstatis1.WorkFinishRateV1Service;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.DateUtils;

@Service
public class WorkFinishRateV1ServiceImpl implements WorkFinishRateV1Service {

    @Autowired
    HomeworkClassForDayReportRepository homeworkClassForDayReportRepository;

    @Override
    public List<RespModel> findFinishRate(String classId, Integer type, String subject, String stage) {
        // type 类型 1-每天， 2-每周， 3-每月
        if (type == 1) { //
            return this.findFinishRateType1(classId, type, subject, stage);
        } else if (type == 2) { //
            return this.findFinishRateType2(classId, type, subject, stage);
        } else if (type == 3) { //
            return this.findFinishRateType3(classId, type, subject, stage);
        }
        return new ArrayList<RespModel>();
    }

    /**
     * 班级每天作业统计
     * <p>
     * 当前时间后推7天
     */
    private List<RespModel> findFinishRateType1(String classId, Integer type, String subject, String stage) {
        // return
        List<RespModel> respModels = new ArrayList<RespModel>();
        // 统计几天
        int count = 8;
        for (int i = 1; i < count; i++) {
            // 当前时间减几天
            Date date = DateUtils.subtractDay(new Date(), i);
            // 0-点时间
            Date beginDate = DateUtils.getFirstDay(date);
            // 24-点时间
            Date endDate = DateUtils.getTimesnight(date);
            // 获取统计数据
            List<HomeworkClassForDayReport> homeworkClassForDayReports = null;
            if (subject == null || subject.equals("0") || subject.trim().length() == 0) {
                homeworkClassForDayReports = homeworkClassForDayReportRepository
                        .findByClassIdAndStatisDateGreaterThanEqualAndStatisDateLessThanEqual(classId, beginDate,
                                endDate, stage);
            } else {
                homeworkClassForDayReports = homeworkClassForDayReportRepository
                        .findByClassIdAndSubjectAndStatisDateGreaterThanEqualAndStatisDateLessThanEqual(classId,
                                subject, beginDate, endDate, stage);
            }
            // return
            RespModel respModel = new RespModel();
            respModel.dataTime = DateToolUtils.format(date, DateToolUtils.FORMAT_SHORT_YMD);
            respModel.completionRate = this.getCompletionRate(homeworkClassForDayReports);
            respModel.excellentRate = this.getExcellentRate(homeworkClassForDayReports);
            respModel.scoreRate = this.getScoreRate(homeworkClassForDayReports);
            respModels.add(respModel);
        }
        return respModels;
    }

    /**
     * 班级每周作业统计
     * <p>
     * 当周时间后推7周
     */
    private List<RespModel> findFinishRateType2(String classId, Integer type, String subject, String stage) {
        // return
        List<RespModel> respModels = new ArrayList<RespModel>();
        // 统计几天
        int count = 7;
        for (int i = 0; i < count; i++) {
            // 当前时间减几周
            Date date = DateUtils.subtractWeek(new Date(), i);
            // 周一 0-点时间
            Date beginDate = DateUtils.getFirstDay(DateUtils.getMondayOfThisWeek(date));
            // 周日 23:59:59 999-点时间
            Date endDate = DateUtils.getTimesnight(DateUtils.getSundayOfThisWeek(date));
            // 获取统计数据
            List<HomeworkClassForDayReport> homeworkClassForDayReports = null;
            if (subject == null || subject.equals("0") || subject.trim().length() == 0) {
                homeworkClassForDayReports = homeworkClassForDayReportRepository
                        .findByClassIdAndStatisDateGreaterThanEqualAndStatisDateLessThanEqual(classId, beginDate,
                                endDate, stage);
            } else {
                homeworkClassForDayReports = homeworkClassForDayReportRepository
                        .findByClassIdAndSubjectAndStatisDateGreaterThanEqualAndStatisDateLessThanEqual(classId,
                                subject, beginDate, endDate, stage);
            }
            // return
            RespModel respModel = new RespModel();
            respModel.dataTime = DateToolUtils.format(date, DateToolUtils.FORMAT_SHORT_Y) + " 第"
                    + DateUtils.getWeekOfYear(date) + "周";
            respModel.completionRate = this.getCompletionRate(homeworkClassForDayReports);
            respModel.excellentRate = this.getExcellentRate(homeworkClassForDayReports);
            respModel.scoreRate = this.getScoreRate(homeworkClassForDayReports);
            respModels.add(respModel);
        }
        return respModels;
    }

    /**
     * 班级每月作业统计
     * <p>
     * 当前月份后推6个月
     */
    private List<RespModel> findFinishRateType3(String classId, Integer type, String subject, String stage) {
        // return
        List<RespModel> respModels = new ArrayList<RespModel>();
        // 统计几天
        int count = 6;
        for (int i = 0; i < count; i++) {
            // 当前时间减几个月
            Date date = DateUtils.subtractMonth(new Date(), i);
            // 月初 0-点时间
            Date beginDate = DateUtils.getFirstDay(DateUtils.getMothOfFirstDay(date));
            // 月末 23:59:59 999-点时间
            Date endDate = DateUtils.getTimesnight(DateUtils.getMothOfLastDay(date));
            // 获取统计数据
            List<HomeworkClassForDayReport> homeworkClassForDayReports = null;
            if (subject == null || subject.equals("0") || subject.trim().length() == 0) {
                homeworkClassForDayReports = homeworkClassForDayReportRepository
                        .findByClassIdAndStatisDateGreaterThanEqualAndStatisDateLessThanEqual(classId, beginDate,
                                endDate, stage);
            } else {
                homeworkClassForDayReports = homeworkClassForDayReportRepository
                        .findByClassIdAndSubjectAndStatisDateGreaterThanEqualAndStatisDateLessThanEqual(classId,
                                subject, beginDate, endDate, stage);
            }
            // return
            RespModel respModel = new RespModel();
            respModel.dataTime = DateToolUtils.format(date, DateToolUtils.FORMAT_SHORT_YM);
            respModel.completionRate = this.getCompletionRate(homeworkClassForDayReports);
            respModel.excellentRate = this.getExcellentRate(homeworkClassForDayReports);
            respModel.scoreRate = this.getScoreRate(homeworkClassForDayReports);
            respModels.add(respModel);
        }
        return respModels;
    }

    // 获取完成率
    private Double getCompletionRate(List<HomeworkClassForDayReport> homeworkClassForDayReports) {
        // 校验
        if (homeworkClassForDayReports == null || homeworkClassForDayReports.size() == 0) { return 0d; }
        // ##
        int count = 0;
        Double completionRate = 0d;
        for (HomeworkClassForDayReport forDayReport : homeworkClassForDayReports) {
            count++;
            completionRate = completionRate + forDayReport.getCompletionRate();
        }
        return this.getDouble(completionRate, count);
    }

    // 获取优秀率
    private Double getExcellentRate(List<HomeworkClassForDayReport> homeworkClassForDayReports) {
        // 校验
        if (homeworkClassForDayReports == null || homeworkClassForDayReports.size() == 0) { return 0d; }
        // ##
        int count = 0;
        Double excellentRate = 0d;
        for (HomeworkClassForDayReport forDayReport : homeworkClassForDayReports) {
            count++;
            excellentRate = excellentRate + forDayReport.getExcellentRate();
        }
        return this.getDouble(excellentRate, count);
    }

    // 获取得分率
    private Double getScoreRate(List<HomeworkClassForDayReport> homeworkClassForDayReports) {
        // 校验
        if (homeworkClassForDayReports == null || homeworkClassForDayReports.size() == 0) { return 0d; }
        // ##
        int count = 0;
        Double scoreRate = 0d;
        for (HomeworkClassForDayReport forDayReport : homeworkClassForDayReports) {
            count++;
            scoreRate = scoreRate + forDayReport.getScoreRate();
        }
        return this.getDouble(scoreRate, count);
    }

    // 格式转化
    private Double getDouble(Double d1, int count) {
        if (count == 0) { return 0d; }
        if (d1 <= 0) { return 0d; }
        Double x1 = d1 / count;
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
        double parse = Double.parseDouble(df.format(x1));
        return parse;
    }
}
